<template>
	<view class="list-common-box" :class="name === 'product'?'productName' :''">
		<block v-if="nameType== 2">
			<block v-if="list.length>0">
				<view class="list-common-img" v-for="(item,index) in list" :key="index" @click="goList(item)">
					<image :src="item.image" mode="widthFix" :lazy-load="true" v-if="item.image"></image>
					<view class="list-common-img-word" v-if="!item.image">
						{{item.name}}
					</view>
				</view>
			</block>
			<view class="list-empty" v-else>
				<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png">
				</u-empty>
			</view>

		</block>

		<view class="index-content-img-box" v-if="nameType!= 2">
			<block v-if="goodsBox.length>0">
				<view class="index-content-img" :class="name==='financing'?'index-content-img-fin':''"
					v-for="(item,index) in goodsBox" :key="index" @click="goDetail(item)">
					<image :src="item.image" mode="widthFix" :lazy-load="true"></image>
					<view class="index-content-word" v-if="name==='education' || name==='project'">
						<view class="index-content-img-title u-ellipsis-1">
							{{item.title}}
						</view>
						<view class="index-content-img-subtitle u-ellipsis-1">
							{{item.subtitle}}
						</view>
						<view class="index-content-img-price" v-if="item.price && name!=='project'">
							{{item.price}} <span>{{item.original_price}}</span>
						</view>
					</view>
				</view>
			</block>
			<view class="list-empty" v-if="goodsBox.length===0">
				<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png">
				</u-empty>
			</view>

		</view>
		<view class="" v-if="nameType!= 2 && goodsBox.length>5">
			<u-loadmore :status="status" />
		</view>
	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				list: [],
				name: '',
				goodsObj: {},
				goodsBox: [],
				nameType: '1',
				isloading: false,
				currentPage: 1, //当前分页
				lastPage: 1, //总分页
				total: 0, //总商品数
				perPage: 0, //单页商品数
				status: 'loadmore',
			}
		},
		created() {},
		onLoad(option) {
			console.log(option, "onLoad"); //获取上一个页面传递的数据
			if (option?.name) {
				const titleType = option.name;
				this.name = option.name
				let barTitle = "教育专区";
				switch (titleType) {
					case "education":
						barTitle = '教育专区';
						this.nameType = 1;
						this.getData('1')
						break;
					case "business":
						barTitle = '商务服务';
						this.nameType = 2;
						this.getBus('8')
						break;
					case "product":
						barTitle = '产品专区';
						this.nameType = 2;
						this.getBus('9')
						break;
					case "project":
						barTitle = '项目专区';
						this.nameType = 1;
						this.getData('33')
						break;
					case "financing":
						barTitle = '融资专区';
						this.nameType = 1;
						this.getData('32')
						break;
					default:
						break;
				}
				uni.setNavigationBarTitle({
					title: barTitle
				});

			}
		},
		// 触底的事件
		onReachBottom() {
			let nameType = this.nameType;
			let id= '';
			let name = this.name;
			if (nameType == 1) {
				if (this.currentPage > this.lastPage) {
					this.status = 'nomore'
					return uni.$showMsg('数据加载完毕!')
				}

				this.status = 'loading';
				// 让页码值自增+1
				this.currentPage++
				if(name === 'financing'){
					id = '32'
				} else if(name === 'project'){
					id= '33'
				}
				this.getData(id)
			}
		},
		methods: {
			getData(id) {
				uni.showLoading({
					title: '正在加载中。。。',
					mask: true
				})
				let name = this.name;
				this.$http('goods.lists', {
					category_id: id,
					page: this.currentPage,
					per_page: 10
				}).then(res => {
					if (res.code === 1) {
						let list = res.data.data;
						if(list.length===0){
							this.status = 'nomore'
						}
						this.goodsBox = [...this.goodsBox, ...res.data.data];
						console.log(this.goodsBox, 'this.goodsBox')
						this.lastPage = res.last_page;
						this.total = res.total;
						this.perPage = res.per_page;
					}
				}).finally(() => {
					uni.hideLoading()
				});
			},
			getBus(id) {
				uni.showLoading({
					title: '正在加载中。。。',
					mask: true
				})
				this.$http('Category.index', {
					id: id
				}).then(res => {
					if (res.code === 1) {
						this.list = res.data.children;
					}
				}).finally(() => {
					uni.hideLoading()
				});
			},
			goList(item) {
				let name = this.name;
				if (name === 'project') {
					this.$Router.push({
						path: '/pages/iconpage/detail',
						query: {
							id: item.id,
							showType: '1'
						}
					})
				} else {
					this.$Router.push({
						path: '/pages/iconpage/goods',
						query: {
							id: item.id,
							name: name
						}
					})
				}

			},
			goDetail(item) {
				let name = this.name;
				if (name === 'financing' || name === 'project') {
					this.$Router.push({
						path: '/pages/iconpage/detail',
						query: {
							id: item.id,
							showType: '1'
						}
					})
				} else {
					this.$Router.push({
						path: '/pages/goods/detail',
						query: {
							id: item.id
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.list-empty {
		width: 100%;
		margin: 30% auto 0;
	}

	.list-common-box {
		padding: 30rpx;

		.list-common-img {
			margin-bottom: 30rpx;
			background: #F6F8FA;
			border-radius: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;

			image {
				width: 100%;
				height: 100%;
				border-radius: 30rpx;
			}

			.list-common-img-word {
				color: #fff;
				font-size: 28rpx;
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				width: 200rpx;
				height: 100rpx;
				text-align: center;
				line-height: 100rpx;
			}
		}

		.index-content-img-box {
			display: flex;
			justify-content: space-between;
			border-radius: 30rpx;
			flex-wrap: wrap;
		}

		.index-content-img {
			width: 330rpx;
			margin-bottom: 30rpx;

			image {
				width: 100%;
				height: 330rpx;
				border-radius: 30rpx 30rpx 0rpx 0rpx;
				vertical-align: top;
			}
		}

		.index-content-img-fin {
			image {
				border-radius: 30rpx;
			}
		}

		.index-content-word {
			background-color: #fff;
			padding: 20rpx 20rpx 30rpx;
			border-radius: 0rpx 0rpx 30rpx 30rpx;

			.index-content-img-title {
				font-size: 32rpx;
				color: #001226;
				font-weight: 600;
				
			}

			.index-content-img-subtitle {
				font-size: 24rpx;
				font-weight: 400;
				color: #8E8E8E;
				margin-top: 10rpx;
			}

			.index-content-img-price {
				font-size: 28rpx;
				color: #FF0015;
				font-weight: 600;
				margin-top: 30rpx;
				span {
					font-size: 24rpx;
					color: #B5B5B5;
					margin-left: 10rpx;
					text-decoration: line-through;
					font-weight: 400;
				}

				span::before {
					content: '￥';
					font-size: 20rpx;
				}
			}

			.index-content-img-price::before {
				content: '￥';
				font-size: 24rpx;
			}
		}
	}

	.productName {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.list-common-img {
			width: 330rpx;
			height: 200rpx;
			border-radius: 30rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}
</style>
